import hashlib
import os

from django.contrib.auth.hashers import make_password, check_password
from django.contrib.auth.models import AbstractUser
from django.db import models
from utils.models import CoreModel

STATUS_CHOICES = (
    (0, "禁用"),
    (1, "启用"),
)


class Users(AbstractUser,CoreModel):
    user_id = models.IntegerField(primary_key=True, help_text="用户主键", verbose_name="用户主键")
    password = models.CharField(null=False, max_length=16, verbose_name="密码", help_text="密码")
    user_group = models.IntegerField(help_text="用户组", verbose_name="用户组")
    partner_id = models.IntegerField(help_text="ERP客户号码", verbose_name="ERP客户号码")
    partner_name = models.CharField(null=False, max_length=40, verbose_name="客户名称", help_text="客户名称")
    phone_number = models.CharField(null=False, max_length=11, verbose_name="电话号码", help_text="电话号码")
    openid = models.CharField(null=False, max_length=100, verbose_name="小程序openid", help_text="小程序openid")

    def to_json(self):
        return  {
            "user_id":self.user_id,
            "user_group":self.user_group,
            "partner_id":self.partner_id,
            "partner_name":self.partner_name,
            "phone_number":self.phone_number,
            "openid":self.openid,
            "username":self.username,
            "groups":[],
            "user_permissions":[],
            "creation_by":self.creation_by,
            "creation_on":self.creation_on,
            "change_by":self.change_by,
            "change_on":self.change_on,
        }


    def check_password(self,password):
        is_correct =  check_password(password,self.password)
        if is_correct:
            return True
        else:
            return  False

    class Meta:
        db_table = "users"
        verbose_name = '用户表'
        verbose_name_plural = verbose_name
        ordering = ('-creation_on',)



class OperationLog(CoreModel):
    # id = models.IntegerField(primary_key=True, help_text="操作日志主键", verbose_name="操作日志主键")
    request_username = models.CharField(max_length=50, blank=True, null=True, verbose_name="请求用户",
                                        help_text="请求用户")
    request_modular = models.CharField(max_length=64, verbose_name="请求模块", null=True, blank=True,
                                       help_text="请求模块")
    request_path = models.CharField(max_length=400, verbose_name="请求地址", null=True, blank=True,
                                    help_text="请求地址")
    request_body = models.TextField(verbose_name="请求参数", null=True, blank=True, help_text="请求参数")
    request_method = models.CharField(max_length=8, verbose_name="请求方式", null=True, blank=True,
                                      help_text="请求方式")
    request_msg = models.TextField(verbose_name="操作说明", null=True, blank=True, help_text="操作说明")
    request_ip = models.CharField(max_length=32, verbose_name="请求ip地址", null=True, blank=True,
                                  help_text="请求ip地址")
    request_browser = models.CharField(max_length=64, verbose_name="请求浏览器", null=True, blank=True,
                                       help_text="请求浏览器")
    response_code = models.CharField(max_length=32, verbose_name="响应状态码", null=True, blank=True,
                                     help_text="响应状态码")
    request_os = models.CharField(max_length=64, verbose_name="操作系统", null=True, blank=True, help_text="操作系统")
    json_result = models.TextField(verbose_name="返回信息", null=True, blank=True, help_text="返回信息")
    status = models.BooleanField(default=False, verbose_name="响应状态", help_text="响应状态")

    class Meta:
        db_table = 'operationLog'
        verbose_name = '操作日志'
        verbose_name_plural = verbose_name
        ordering = ('-creation_on',)


def media_file_name(instance, filename):
    h = instance.md5sum
    basename, ext = os.path.splitext(filename)
    return os.path.join('files', h[0:1], h[1:2], h + ext.lower())


class File(CoreModel):
    id = models.IntegerField(primary_key=True, help_text="文件主键", verbose_name="文件主键")
    name = models.CharField(max_length=255, null=True, blank=True, verbose_name="实际名称", help_text="实际名称")
    save_name = models.CharField(max_length=255, null=True, blank=True, verbose_name="存储名称", help_text="存储名称")
    url = models.FileField(upload_to=media_file_name)
    size = models.BigIntegerField(null=True, blank=True, verbose_name="大小", help_text="大小")
    md5sum = models.CharField(max_length=36, blank=True, verbose_name="文件md5", help_text="文件md5")

    class Meta:
        db_table = 'file'
        verbose_name = '文件管理'
        verbose_name_plural = verbose_name
        ordering = ('-creation_on',)

class LoginLog(CoreModel):
    LOGIN_TYPE_CHOICES = (
        (1, '普通登录'),
    )
    # id = models.IntegerField(primary_key=True,  help_text="日志主键", verbose_name="日志主键")
    username = models.CharField(max_length=16, verbose_name="登录用户名", null=True, blank=True, help_text="登录用户名")
    ip = models.CharField(max_length=32, verbose_name="登录ip", null=True, blank=True, help_text="登录ip")
    agent = models.TextField(verbose_name="agent信息", null=True, blank=True, help_text="agent信息")
    browser = models.CharField(max_length=200, verbose_name="浏览器名", null=True, blank=True, help_text="浏览器名")
    os = models.CharField(max_length=200, verbose_name="操作系统", null=True, blank=True, help_text="操作系统")
    continent = models.CharField(max_length=50, verbose_name="州", null=True, blank=True, help_text="州")
    country = models.CharField(max_length=50, verbose_name="国家", null=True, blank=True, help_text="国家")
    province = models.CharField(max_length=50, verbose_name="省份", null=True, blank=True, help_text="省份")
    city = models.CharField(max_length=50, verbose_name="城市", null=True, blank=True, help_text="城市")
    district = models.CharField(max_length=50, verbose_name="县区", null=True, blank=True, help_text="县区")
    isp = models.CharField(max_length=50, verbose_name="运营商", null=True, blank=True, help_text="运营商")
    area_code = models.CharField(max_length=50, verbose_name="区域代码", null=True, blank=True, help_text="区域代码")
    country_english = models.CharField(max_length=50, verbose_name="英文全称", null=True, blank=True,
                                       help_text="英文全称")
    country_code = models.CharField(max_length=50, verbose_name="简称", null=True, blank=True, help_text="简称")
    longitude = models.CharField(max_length=50, verbose_name="经度", null=True, blank=True, help_text="经度")
    latitude = models.CharField(max_length=50, verbose_name="纬度", null=True, blank=True, help_text="纬度")
    login_type = models.IntegerField(default=1, choices=LOGIN_TYPE_CHOICES, verbose_name="登录类型",
                                     help_text="登录类型")

    class Meta:
        db_table = 'loginLog'
        verbose_name = '登录日志'
        verbose_name_plural = verbose_name
        ordering = ('-creation_on',)

